Page({
  data: {
    tabIndex: 'hots',
    limit:12,
    page:1,
  },
  onLoad: function() {
    var self = this;
    wx.getLocation({
      success: function(res) {
        var latitude = res.latitude.toFixed(6);
        var longitude = res.longitude.toFixed(6);
        wx.request({
          url: 'https://wx.maoyan.com/hostproxy/locate/v2/rgeo',
          data: {
            coord: [latitude, longitude, 1].join(','),
          },
          header: {
            'x-host': 'http://apimobile.vip.sankuai.com',
          },
          method: 'get',
          success: function(info) {
            self.setData({
              city: info.data.data.city
            });
            wx.request({
              url: 'https://wx.maoyan.com/mmdb/movie/v2/list/hot.json',
              data: {
                ct: info.data.data.city,
                limit: 12,
                offset: 0,
              },
              method: 'get',
              success: function(hots) {
                console.log(hots.data.data.hot);
                hots.data.data.hot.forEach(function(val) {
                  val.img = val.img.replace('w.h', '128.180');
                });
                self.setData({
                  hots: {
                    items: hots.data.data.hot,
                  },
                });
              },
            })
          },
        });
      },
    });
  },
  onReachBottom:function(){
    var self = this;
    var hasMore = self.data.hots.hasMore;
    if(!hasMore) return;
    console.log(this.data);
    wx.request({
      url: 'https://wx.maoyan.com/mmdb/movie/v2/list/hot.json',
      data:{
        ct:self.data.city,
        limit:self.data.limit,
        offset:self.data.limit * self.data.page
      },
      method:'get',
      success:function(info){
        info.data.data.hot.forEach(function (val){
          val.img = val.img.replace('w.h','128.130');
        });
        // 将新请求来的数据追加至原有数据中
        var items = self.data.hots.items.concat(info.data.data.hot);
        self.setData({
          page:++self.data.page,
          hots:{
            items:items,
            hasMore: info.data.data.paging.hasMore,
          },
        });
      },
    });
  },
  // onPullDownRefresh: function () {
  //   console.log('有人下拉了...');

  //   var self = this;
  //   wx.request({
  //     url: 'https://wx.maoyan.com/mmdb/movie/v2/list/hot.json',
  //     data: {
  //       ct: self.data.city,
  //       limit: 12,
  //       offset: 0
  //     },
  //     method: 'get',
  //     success: function (info) {
  //       info.data.data.hot.forEach(function (val) {
  //         val.img = val.img.replace('w.h', '128.180');
  //       })

  //       self.setData({
  //         hots: {
  //           items: info.data.data.hot,
  //           // 有没有下一页
  //           hasMore: info.data.data.paging.hasMore
  //         },
  //         // 刷新后页码恢复到 第 1 页
  //         page: 1
  //       });
  //     }
  //   });
  // },
  switch: function(ev) {
    // console.log(ev);
    var tabIndex = ev.target.dataset.tabIndex;
    this.setData({
      tabIndex: tabIndex
    });
  }
  
});